package com.dst.smart.tools;

import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;



/**
 * 热点创建相关工具类
 * @author 奎华
 *
 */
public class HPaConnector {

	private static final String SETUP_WIFIAP_METHOD = "setWifiApEnabled";
	private static final String def_psw="12345678";
	private static final int SECURITY_NONE = 0;
	private static final int SECURITY_WEP = 1;
	private static final int SECURITY_PSK = 2;
	Context context = null;
	WifiManager wifiManager = null;
	static HPaConnector hPaConnector = null;
	Boolean isEnableNetWork = false;
	

	public static HPaConnector getInstance(Context context) {
		if (hPaConnector == null) {
			hPaConnector = new HPaConnector();
			hPaConnector.context = context.getApplicationContext();
			hPaConnector.wifiManager = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
		}
		return hPaConnector;
	}
	
	/**
	 * 是否存在指定的ssid
	 * @param sSID
	 * @return
	 */
	private WifiConfiguration IsExsits(String sSID) {
		// TODO Auto-generated method stub
		WifiConfiguration result = null;
		Log.d("HPaConnector", "IsExsits: " + sSID);
		if (wifiManager != null) {
			List<WifiConfiguration> configuredNetworks = wifiManager
					.getConfiguredNetworks();
			if(configuredNetworks!=null){
				for (WifiConfiguration config : configuredNetworks) {
					if (config != null && config.SSID.contains("\"" + sSID + "\"")) {
						result = config;
						break;
					}
				}
			}
		}
		return result;
	}
	/**
	 * 连接热点
	 * @param SSID
	 * @return
	 */
	public boolean connectToAp(String SSID)
	{
		return ConnectToAp(SSID, def_psw);
	}
	public boolean ConnectToAp(String SSID, String Password)
	{
		return ConnectToAp(SSID, Password, SECURITY_PSK);
	}
	/**
	 * 连接指定热点
	 * @param SSID
	 * @param Password
	 * @param Type
	 * @return
	 */
	public boolean ConnectToAp(String SSID, String Password, int Type) {
		
		Log.e("HPaConnector", "< -------------- 开始连接热点  ");
		WifiConfiguration wifiConfig;
		int netID;
		//是否存在ap ssid
		WifiConfiguration tempConfig = IsExsits(SSID);
		if (tempConfig != null) {//存在 apssid
//			wifiConfig = tempConfig;
//			netID = wifiConfig.networkId;
//			Log.d("HPaConnector", "使用已有的 ap网络配置，网络id："+netID);
			wifiManager.removeNetwork(tempConfig.networkId);
		}
		
		{//不存在ssid，则创建
			Log.d("HPaConnector", "创建新的ap网络配置");
			wifiConfig = createWifiInfo(SSID, Password, Type);
			if (wifiConfig == null) {
				Log.v("HPaConnector", "wifiConfig == null");
				return false;
			}
			netID = wifiManager.addNetwork(wifiConfig);
		}
		//是一个异步事件
		boolean bRet = wifiManager.enableNetwork(netID, true);
		Log.e("HPaConnector", "开始连接Ap netID = " + netID+" enable result:"+bRet);// 连不加密AP总是返回-1
		isEnableNetWork = bRet;
		Log.e("HPaConnector", "< -------------- 连接完成 ---------------> "+isEnableNetWork);
		return isEnableNetWork;
	}
	
	/**
	 * 移除ap配置
	 * @param ssid
	 */
	public void removeAp(String ssid){
		WifiConfiguration tempConfig = IsExsits(ssid);
		if (tempConfig != null) {//存在 apssid
			wifiManager.enableNetwork(tempConfig.networkId, false);
			wifiManager.removeNetwork(tempConfig.networkId);
		}
	}

	/**
	 * 创建热点
	 * @param SSID
	 * @param password
	 * @param type
	 * @return
	 */
	private WifiConfiguration createWifiInfo(String SSID, String password,
			int type) {

		Log.e("HPaConnector", "SSID = " +SSID + "## Password = " + password
				+ "## Type = " + type);

		WifiConfiguration config = new WifiConfiguration();
		config.allowedAuthAlgorithms.clear();
		config.allowedGroupCiphers.clear();
		config.allowedKeyManagement.clear();
		config.allowedPairwiseCiphers.clear();
		config.allowedProtocols.clear();
		config.SSID = "\"" + SSID + "\"";


		// 分为三种情况：没有密码 用wep加密 用wpa加密
		if (type == SECURITY_NONE) { // WIFICIPHER_NOPASS
			config.wepKeys[0] = "";
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.wepTxKeyIndex = 0;

		} else if (type == SECURITY_WEP) { // WIFICIPHER_WEP
			config.hiddenSSID = true;
			config.wepKeys[0] = "\"" + password + "\"";
			config.allowedAuthAlgorithms
					.set(WifiConfiguration.AuthAlgorithm.SHARED);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			config.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.WEP104);
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.wepTxKeyIndex = 0;
		} else if (type == SECURITY_PSK) { // WIFICIPHER_WPA
			config.preSharedKey = "\"" + password + "\"";
			config.hiddenSSID = true;
			config.allowedAuthAlgorithms
					.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
			config.allowedPairwiseCiphers
					.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
			config.allowedPairwiseCiphers
					.set(WifiConfiguration.PairwiseCipher.CCMP);
			config.status = WifiConfiguration.Status.ENABLED;
		}
		return config;
	}

}
